home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93c.txt / 000118_icon-group-sender _Wed Dec 15 15:46:59 1993.msg < prev    next >
Internet Message Format  |  1994-02-02  |  3KB

  1. Received: by cheltenham.cs.arizona.edu; Wed, 15 Dec 1993 18:26:11 MST
  2. Message-Id: <9312152344.AA26970@enlil.premenos.sf.ca.us>
  3. From: Ken Walker <kwalker@shara.premenos.sf.ca.us>
  4. Subject: Re: Need help with (simple?) programming problem.
  5. To: icon-group@cs.arizona.edu
  6. Date: Wed, 15 Dec 93 15:46:59 PST
  7. Mailer: Elm [revision: 66.25]
  8. Status: R
  9. Errors-To: icon-group-errors@cs.arizona.edu
  10.  
  11. > From Ronal F. Guilmette:
  12. >
  13. > Assume that I have three strings, i.e. "red ", "green ", and "blue ". Now
  14. > I want to create a generator which will successively yield each unique
  15. > string which contains no more than one instance of each of the original
  16. > strings.  Here are the string values which should be yielded by the various
  17. > invocations/resumptions of the generator:
  18. >         ""
  19. >         "red "
  20. >         "green "
  21. >         "blue "
  22. >         "red green "
  23. >         "red blue "
  24. >         "green red "
  25. >         "green blue "
  26. >         "blue red "
  27. >         "blue green "
  28. >         "red green blue "
  29. >         "red blue green "
  30. >         "green red blue "
  31. >         "green blue red "
  32. >         "blue red green "
  33. >         "blue green red "
  34. > After being invoked/resumed 16 times (and yielding each of the above strings
  35. > once) the generator should fail.  Note that the actual *order* in which the
  36. > above strings are yielded is totally unimportant.  Any order will do as long
  37. > as I get these 16 strings (eventually).
  38. > Also note that the ORDER in which the original strings appear in the genera-
  39. > ted strings *is* significant.
  40.  
  41. I found the hard part was formulating the problem in "constructive"
  42. terms. The problem, as I understand it, is to generate all the permutaions
  43. of all the subsets of a list of words. Once it is formulated that way,
  44. it is simple to solve using two recursive generators (permutaions has
  45. certainly been done using this technique before, and I suspect subsets
  46. has also). Here is a solution using list invocation:
  47.  
  48.    procedure main()
  49.       p("red", "blue", "green")
  50.    end
  51.    
  52.    procedure p(words[])
  53.       every write(permute ! (subsets ! words))
  54.    end
  55.    
  56.    procedure subsets(words[])
  57.       local i, w, tail
  58.    
  59.       suspend []
  60.       every i := 1 to *words do {
  61.          w := words[i : i + 1]
  62.          tail := words[i + 1 : 0]
  63.          suspend w ||| (subsets ! tail)
  64.          }
  65.    end
  66.    
  67.    procedure permute(words[])
  68.       local i, head, w, tail
  69.    
  70.       if *words = 0 then
  71.         return ""
  72.       every i := 1 to *words do {
  73.          head := words[1 : i] 
  74.          w := words[i] 
  75.          tail := words[i + 1 : 0]
  76.          suspend w || " " || (permute ! (head ||| tail))
  77.          }
  78.    end
  79.    
  80.    
  81. Ken Walker, kwalker@premenos.sf.ca.us
  82.  
  83.